package com.springboot.chongwu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.chongwu.entity.Lunbotu;
import com.springboot.chongwu.entity.Yonghu;
import com.springboot.chongwu.mapper.YonghuMapper;
import com.springboot.chongwu.qpage.QPage;
import com.springboot.chongwu.service.IYonghuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.chongwu.util.ResultView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-09-24
 */
@Service
public class YonghuServiceImpl extends ServiceImpl<YonghuMapper, Yonghu> implements IYonghuService {

    @Override
    public Yonghu login(String account, String mima) {
        QueryWrapper<Yonghu> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("account",account);
        queryWrapper.eq("mima", mima);
        Yonghu yonghu=this.getOne(queryWrapper);
        return yonghu;
    }

    @Override
    public boolean exitAccount(String account) {
        QueryWrapper<Yonghu> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("account",account);
        List<Yonghu> yonghuList=this.list(queryWrapper);
        if(yonghuList!=null&&yonghuList.size()>=1){
            return true;
        }
        return false;
    }

    @Override
    public ResultView getPageList(QPage qPage, Yonghu obj) {
        IPage iPage = new Page(qPage.getOffset(), qPage.getLimit());
        QueryWrapper<Yonghu> queryWrapper=new QueryWrapper<>();
        if(StringUtils.isNotBlank(obj.getNickname())){
            queryWrapper.like("nickname",obj.getNickname());
        }
        if(StringUtils.isNotBlank(obj.getAccount())){
            queryWrapper.like("account",obj.getAccount());
        }
        queryWrapper.orderByAsc("ctime");
        iPage = this.baseMapper.selectPage(iPage,queryWrapper);
        return ResultView.ok(iPage);
    }

    @Transactional
    @Override
    public ResultView add(Yonghu obj) {
        QueryWrapper<Yonghu> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("account",obj.getAccount());
        List<Yonghu> yonghuList=this.list(queryWrapper);
        if(yonghuList!=null&&yonghuList.size()>=1){
            return ResultView.error("用户账号已存在");
        }
        obj.setCtime(new Date());
        obj.setUtime(new Date());
        this.save(obj);
        return ResultView.ok();
    }

    @Transactional
    @Override
    public ResultView del(String id) {
        this.removeById(id);
        return ResultView.ok();
    }

    @Transactional
    @Override
    public ResultView up(Yonghu obj) {
        obj.setUtime(new Date());
        this.updateById(obj);
        return ResultView.ok();
    }


}
